﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Item_NewItemCategory : System.Web.UI.Page
{

    //Page ID=401
    Datautility dut = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
           Response.Redirect("~/SessionExpired.aspx");
        }
        string Pagepermission = "select PAGE_ID,PAGE_ADD,PAGE_VIEW,USERID,USERNAME as EmailID from USER_PERMISSION inner join LOGIN on USER_PERMISSION.USER_ID=LOGIN.USERID where USER_ID='" + Session["userid"].ToString() + "' and PAGE_ID=401";
        DataTable dt = dut.getdata(Pagepermission);
        if (dt.Rows.Count > 0)
        {
            if (Request.QueryString["action"] != null)
            {
                if (Request.QueryString["action"].ToString() == "addnew")
                {
                    if (dt.Rows[0]["PAGE_ADD"].ToString() == "True")
                    {
                        PnlAddNew.Visible = true;
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }
                else if (Request.QueryString["action"].ToString() == "view")
                {
                    if (dt.Rows[0]["PAGE_VIEW"].ToString() == "True")
                    {
                        PnlView.Visible = true;
                        if (!IsPostBack)
                        {
                            BindCategory();
                        }
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }
            }
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

    protected void Insert_Click(object sender, EventArgs e)
    {
        string itemcategory_name = TxtCategoryName.Text;
        string sql = "select ICAID from IVR_ITEMCATEGORY where ICANAME = '" + itemcategory_name + "'";
        DataTable DT = dut.getdata(sql);
        if (DT.Rows.Count > 0)
        {
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Category already exists! ";
        }
        else
        {
            SqlParameter[] parm ={
                                  new SqlParameter("@ICANAME", TxtCategoryName.Text.Trim()),                          
                                  new SqlParameter("@CName",Session["username"].ToString()),
                                  new SqlParameter("@COMPANYID","1")
                               
                             };
            int j= dut.executeprocedure("usp_insert_IVR_ITEMCATEGORY", parm);
            if (j > 0)
            {
                TxtCategoryName.Text = "";
                lblMsg.Visible = true;
                lblMsg.ForeColor = System.Drawing.Color.Green;
                lblMsg.Text = "Category Created Successfully...";
                BindCategory();
            }
            else
            {
                lblMsg.Visible = true;
                lblMsg.ForeColor = System.Drawing.Color.Red; 
                lblMsg.Text = "Error While Category Created ...";
            }
        }
    }

    public void BindCategory()
    {
        DataTable DT = dut.getdata("select ICAID, ICANAME, ICAPREDEF from IVR_ITEMCATEGORY");
        DataView dv;
        if (ViewState["sortExpr"] != null)
        {
            dv = new DataView(DT);
            dv.Sort = (string)ViewState["sortExpr"] + " " + GetSortDirection((string)ViewState["sortExpr"]);
        }
        else
        {
            dv = DT.DefaultView;
        }
        GridCategory.DataSource = dv;
        GridCategory.DataBind();
        custPager.TotalPages = 2; 
    }
    protected void GridCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridCategory.EditIndex = e.NewEditIndex;
        BindCategory();
        GridCategory.Rows[e.NewEditIndex].Cells[3].Controls[1].Visible = false;
        GridCategory.Rows[e.NewEditIndex].Cells[3].Controls[3].Visible = true;
        GridCategory.Rows[e.NewEditIndex].Cells[3].Controls[5].Visible = true;
    }
    protected void GridCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridCategory.EditIndex = -1;
        BindCategory();
    }
    protected void GridCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox Txtname = (TextBox)GridCategory.Rows[e.RowIndex].Cells[1].Controls[0];
        if (Txtname.Text.Trim() != string.Empty)
        {
            string key = GridCategory.DataKeys[e.RowIndex].Value.ToString();
            string sql = "select ICAID from IVR_ITEMCATEGORY where ICANAME = '" + Txtname.Text + "' and ICAID !=" + key;
            DataTable DT = dut.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Category already exists! ";
            }
            else
            {
                SqlParameter[] parm ={
                               new SqlParameter("ICANAME",Txtname.Text.Trim()),
                               new SqlParameter("ICAID",GridCategory.DataKeys[e.RowIndex].Value.ToString()),
                               new SqlParameter("@EName",Session["username"].ToString()) ,
                               new SqlParameter("@COMPANYID","1")
                             };
                int j=dut.executeprocedure("usp_update_IVR_ITEMCATEGORY", parm);
                GridCategory.EditIndex = -1;
                BindCategory();
                if (j > 0)
                {
                    lblMsg.Visible = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;                    
                    lblMsg.Text = "Category details updated successfully...";
                }
                else
                {
                    lblMsg.Visible = true;
                    lblMsg.ForeColor = System.Drawing.Color.Red; 
                    lblMsg.Text = "Error While Category details updated ...";
                }
            }
        }
    }

    protected void GridCategory_RowDataBound1(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string pre = e.Row.Cells[2].Text;
            if (pre == "True")
            {
                e.Row.Cells[3].Controls[1].Visible = false;
                e.Row.Cells[3].Controls[7].Visible = true;
                //lblMsg.Text += e.Row.Cells[3].Controls[1].ID;
            }
            else
            {
                e.Row.Cells[3].Controls[1].Visible = true;
                e.Row.Cells[3].Controls[7].Visible = false;
            }
            //else
            //{
            //    e.Row.Cells[3].Controls[1].Visible = true;
            //}
        }
    }

    protected void GridCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridCategory.PageIndex = e.NewPageIndex;
        BindCategory();

    }

    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        GridCategory.PageSize = e.CurrentPageSize;
        GridCategory.PageIndex = e.CurrentPageNumber;
        BindCategory();
    }

    protected void GridCategory_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
   
    protected void GridCategory_Sorting(object sender, GridViewSortEventArgs e)

    {
        ViewState["sortExpr"] = e.SortExpression;
        BindCategory();
    }

    private string GetSortDirection(string column)
    {

        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }
}
